<?php

class BasePage {

    /**
     * 判断浏览器
     * @return string
     * 
     */
    static function getBrowser() {
        $agent = $_SERVER["HTTP_USER_AGENT"];
        if (strpos($agent, 'MSIE') !== false || strpos($agent, 'rv:11.0')) { //ie11判断
            return "IE";
        } else if (strpos($agent, 'Firefox') !== false) {
            return "火狐";
        } else if (strpos($agent, 'Chrome') !== false) {
            return "谷歌";
        } else if (strpos($agent, 'Opera') !== false) {
            return '欧朋';
        } else if ((strpos($agent, 'Chrome') == false) && strpos($agent, 'Safari') !== false) {
            return '苹果';
        } else {
            return '其他';
        }
    }

    /**
     * 判断浏览器版本号
     * @return string
     */
    static function getBrowserVer() {
        //当浏览器没有发送访问者的信息的时候
        if (empty($_SERVER['HTTP_USER_AGENT'])) {
            return '其他';
        }
        $agent = $_SERVER['HTTP_USER_AGENT'];
        if (preg_match('/MSIE\s(\d+)\..*/i', $agent, $regs)) {
            return $regs[1];
        } elseif (preg_match('/FireFox\/(\d+)\..*/i', $agent, $regs)) {
            return $regs[1];
        } elseif (preg_match('/Opera[\s|\/](\d+)\..*/i', $agent, $regs)) {
            return $regs[1];
        } elseif (preg_match('/Chrome\/(\d+)\..*/i', $agent, $regs)) {
            $str = explode("/", $regs[0]);
            $aa = (explode(" ", $str[1]));
            return $aa[0];
        } elseif ((strpos($agent, 'Chrome') == false) && preg_match('/Safari\/(\d+)\..*$/i', $agent, $regs)) {
            return $regs[1];
        } else {
            return '其他';
        }
    }

    /**
     * 用户操作消息提示
     * @param string $status right|wrong|warning
     * @param string $msg 提示信息
     * @param string $href A标签的链接地址
     * @param string $target 打开方式 _blank|_top|_self|_parent
     */
    static protected function getMsg($status, $message, $href, $time = 5, $target = '_self') {
        $_SESSION['msg']['status'] = $status;
        $_SESSION['msg']['message'] = $message;
        $_SESSION['msg']['href'] = $href;
        $_SESSION['msg']['time'] = $time;
        $_SESSION['msg']['target'] = $target;
        /**
          setcookie('msg[status]', $status, time() + $time, ADMIN_FOLDER, DOMAIN_NAME);
          setcookie('msg[message]', $message, time() + $time, ADMIN_FOLDER, DOMAIN_NAME);
          setcookie('msg[href]', $href, time() + $time, ADMIN_FOLDER, DOMAIN_NAME);
          setcookie('msg[time]', $time, time() + $time, ADMIN_FOLDER, DOMAIN_NAME);
          setcookie('msg[target]', $target, time() + $time, ADMIN_FOLDER, DOMAIN_NAME);
         * 
         */
        header('location: ' . DOMAIN_NAME_ADMIN . '/index.php?C=Login&A=msgInfo');
        exit;
    }

    //当前主菜单ID
    static protected $main_menu_id = 1;
    //当前子菜单ID
    static protected $child_menu_id = 0;
    //当前二级菜单ID
    static protected $second_menu_id = 0;
    //所有顶级菜单
    static protected $main_menu_list = array();
    //当前页面的所有子菜单
    static protected $child_menu_list = array();
    //当前页面的主菜单信息
    static private $current_menu_info;

    /**
      +--------------------------------------------------------------------------
     * 获取后台菜单
      +--------------------------------------------------------------------------
     * @author 
     * @copyright 
     */
    static protected function getMenuList($obj_conn) {
        $class_name = isset($_GET['C']) ? trim($_GET['C']) : '';
        if (empty($class_name)) {
            return false;
        }
        $function_name = isset($_GET['A']) ? trim($_GET['A']) : '';
        $way_name = isset($_GET['way']) ? trim($_GET['way']) : '';
        $current_menu = $obj_conn->getOneRow('tb_menu', 'Fid,FcateId,FcateIds', "WHERE Flevel=3 AND Fclass='$class_name' AND Ffunction='$function_name' AND Fway='$way_name'");
        self::$current_menu_info = $current_menu;
        if (isset($current_menu['Fid'])) {
            $current_menu_arr = explode(',', $current_menu['FcateIds']);
            if (count($current_menu_arr) > 1) {
                $main_menu_id = $current_menu_arr[1];
            } else {
                $main_menu_id = $current_menu_arr[0];
            }
            self::$main_menu_id = $main_menu_id;
            self::$child_menu_id = $current_menu['Fid'];
            self::$second_menu_id = $current_menu['FcateId'];
        } else {
            //self::getMsg('wrong', '对不起，您还没有添加当前请求的页面菜单，请先添加然后再访问该页面。', DOMAIN_NAME_ADMIN . 'index.php?C=System&A=menuInfo&way=A');
            $main_menu_id = 1;
        }
        self::$main_menu_list = $obj_conn->getList('tb_menu', 'Fid,Fname,FcateId,Fclass,Ffunction,Fway', "WHERE FcateId=0 ", 'ORDER BY Fsort');
        $menu_list = $obj_conn->getList('tb_menu', 'Fid,Fname,FcateId,Fclass,Ffunction,Fway,Fsort', "WHERE FIND_IN_SET($main_menu_id,FcateIds) AND Flevel>1 AND Fshow=1", 'ORDER BY FcateId,Fsort');
        //重新构造数组
        foreach ($menu_list as $ls) {
            self::$child_menu_list[$ls['FcateId']][$ls['Fid']] = $ls;
        }
    }

    /**
     * 判断用户是否登录
     */
    static protected function checkUserLogin() {

        // 判断用户存在并且用户id为整数
        if (isset($_COOKIE['admin']['user'])) {
            $user_id = BaseYspCookie::getCookie($_COOKIE['admin']['id']);
            if (is_numeric($user_id) && $user_id > 0) {

                header('location: ' . 'index.php?C=Login&A=login');
            } else {
                header('location: ' . DOMAIN_NAME_ADMIN);
            }
        } else {
            return FALSE;
        }
    }

}
